home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6813 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: 'Freeing' storage with realloc
  5. Date: 15 Feb 1996 13:34:19 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4fvcor$okv@sparcserver.lrz-muenchen.de>
  9. References: <4fv44f$p2v@news.rz.uni-passau.de>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11. Keywords: realloc, memory-managment
  12.  
  13. berndl@sidonius.uni-passau.de (Klaus Berndl) writes:
  14.  
  15.  
  16. >Please look at the snipped of code:
  17.  
  18. >#include <stdio.h>
  19.  
  20. >main () {
  21.  
  22. >  char* buf = NULL;
  23.  
  24. >  buf = (char*)malloc(100);
  25.  
  26. This is a real problem. There is no prototype for malloc() in your
  27. program, and your compiler cannot warn you that you are trying to
  28. assign an int to a char * because the cast hides that error.
  29.  
  30. >  strcpy(buf, "Klaus Berndl");
  31. >  printf("\n%s\n", buf);
  32.  
  33. >/* line 10 */  buf = (char*)realloc(buf, strlen(buf)+1);  /* line 10 */
  34.  
  35. >  if (buf)
  36. >    printf("\n%s\n", buf);
  37. >  else
  38. >    printf("\nshit!\n");
  39. >}
  40.  
  41. >My questions are now: How much memory is allocated for 'buf' after
  42. >executing line 10?! Does realloc 'freeing' the storage behind byte 13?
  43. >Or are still 100 bytes allocated for buf?
  44.  
  45. You cannot access buf[14] after line 10 in all implementations of C.
  46. Whether "freed" memory is available for further allocation requests
  47. of your program or other running programs depends on the implementation
  48. of your free storage management, so there is no general answer to
  49. this question. All reasonable implementations will at least make 
  50. "freed" storage avaiable for further allocation requests from _your_
  51. program. 
  52.  
  53. Kurt
  54. -- 
  55. | Kurt Watzka                             Phone : +49-89-2180-6254
  56. | watzka@stat.uni-muenchen.de
  57. | ua302aa@sunmail.lrz-muenchen.de
  58.